﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices.ComTypes;
using System.Text;
using System.Threading.Tasks;
using DataBaseLibrary.DBInterfaces;
using System.ComponentModel;

namespace DataBaseLibrary.DBObjectsFB
{
    public class SalesContract : DBFObject, IDBObject, INotifyPropertyChanged
    {

        private int m_id_saler;
        private int m_id_buyer;
        private int m_id_vehicle;


        String m_NumberSalesContract = "";
        ///Номер договора к/п. (ID)
        public String NumberSalesContract
        {
            get
            {
                return m_NumberSalesContract;
            }
            set
            {
                m_NumberSalesContract = value;
                OnPropertyChanged(new PropertyChangedEventArgs("NumberSalesContract"));
            }
        }

        /// <summary>
        /// Дата создания договора
        /// </summary>
        private DateTime mDateCreateContract = DateTime.MinValue;
        public DateTime DateCreateContract 
        {
            set { mDateCreateContract = value; OnPropertyChanged(new PropertyChangedEventArgs("DateCreateContract")); }
            get { return mDateCreateContract; } 
        }

        /// <summary>
        /// Представитель продавца
        /// </summary>
        private int mRepresentativeSaler;
        public Representative RepresentativeSaler { set; get; }

        /// <summary>
        /// Представитель покупателя
        /// </summary>
        private int mRepresentativeBuyer;
        public Representative RepresentativeBuyer { set; get; } 

        /// <summary>
        /// Продавец
        /// </summary>
        public Sallers Saler { set; get; } 

        /// <summary>
        /// Покупатель
        /// </summary>
        public Buyers Buyer { set; get; }

        /// <summary>
        /// Транспортное средство
        /// </summary>
        public Vechicle Vehicle { set; get; }

        /// <summary>
        /// Цена
        /// </summary>
        private double mPrice = 0.00;
        public double Price 
        {
            set { mPrice = value; OnPropertyChanged(new PropertyChangedEventArgs("Price")); }
            get { return mPrice; } 
        }

        public override string ToString()
        {
            return String.Format("Договор купли-продажи №{0}", NumberSalesContract);
        }

        public SalesContract()
        {
            NameTable = "SALE_CONTRACT_TABLE";

            Saler = new Sallers();
            Buyer = new Buyers();
            RepresentativeSaler = new Representative();
            RepresentativeBuyer = new Representative();
            Vehicle = new Vechicle();

            DateCreateContract = DateTime.Now;
        }

        object IDBObject.Save()
        {
            try
            {

                Parameters = new Dictionary<string, object>();


                Parameters.Add("ID_SALER", (Saler as IDBObject).Save());
                Parameters.Add("ID_BUYER", (Buyer as IDBObject).Save());
                Parameters.Add("ID_REPRESENTATIVE_SALER", (RepresentativeSaler as IDBObject).Save());
                Parameters.Add("ID_REPRESENTATIVE_BAYER", (RepresentativeBuyer as IDBObject).Save());
                Parameters.Add("ID_VEHICLE", (Vehicle as IDBObject).Save());
                Parameters.Add("PRICE", Price);
                Parameters.Add("DATE_CREATE_SALE", DateCreateContract);
                NumberSalesContract = SaveObject() as String;
                return NumberSalesContract;
            }
            catch (SystemException)
            {
                throw new SystemException(String.Format("Ошибка при сохранении объекта {0} в БД", this.ToString()));
            }
        }

        void IDBObject.Load(object ID)
        {
            try
            {

                Parameters = new Dictionary<string, object>();
                Parameters.Add("ID_SALER", m_id_saler);
                Parameters.Add("ID_BUYER", m_id_buyer);
                Parameters.Add("ID_REPRESENTATIVE_SALER", mRepresentativeSaler);
                Parameters.Add("ID_REPRESENTATIVE_BAYER", mRepresentativeBuyer);
                Parameters.Add("ID_VEHICLE", m_id_vehicle);
                Parameters.Add("PRICE", Price);
                Parameters.Add("DATE_CREATE_SALE", DateCreateContract);

                LoadObject(ID);
                ((Saler = new Sallers()) as IDBObject).Load(Parameters["ID_SALER"]);
                ((Buyer = new Buyers()) as IDBObject).Load(Parameters["ID_BUYER"]);
                ((RepresentativeSaler = new Representative()) as IDBObject).Load(Parameters["ID_REPRESENTATIVE_SALER"]);
                ((RepresentativeBuyer = new Representative()) as IDBObject).Load(Parameters["ID_REPRESENTATIVE_BAYER"]);
                ((Vehicle = new Vechicle()) as IDBObject).Load(Parameters["ID_VEHICLE"]);
                Price = Convert.ToDouble(Parameters["PRICE"]);
                DateCreateContract =  DateTime.Parse( Parameters["DATE_CREATE_SALE"].ToString() );
                NumberSalesContract = ID.ToString();
            }
            catch (SystemException err)
            {
                NumberSalesContract = "";
                throw new SystemException(String.Format("Ошибка при загрузке объекта {0} в БД с сообщением: {1}", this.ToString(), err.Message));
            }
        }


        void IDBObject.Update()
        {
            
            (Saler as IDBObject).Update();
            (Buyer as IDBObject).Update();
            (RepresentativeSaler as IDBObject).Update();
            (RepresentativeBuyer as IDBObject).Update();
            (Vehicle as IDBObject).Update();

            Parameters = new Dictionary<string, object>();
            Parameters.Add("PRICE", Price);
            Parameters.Add("DATE_CREATE_SALE", DateCreateContract);
            UpdateObject();
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, e);
        }
    }

}
